package com.example.gzysmartcity.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.ActivityStartBinding;
import com.example.gzysmartcity.databinding.PopuStartBinding;
import com.example.gzysmartcity.ui.adapter.StartVPAdapter;
import com.example.gzysmartcity.utils.PopupUntil;

import java.util.ArrayList;
import java.util.List;

public class StartActivity extends AppCompatActivity {
    private SharedPreferences sp;
    private final List<String> title = new ArrayList<>();

    private final List<Integer> integers = new ArrayList<>();

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
//        switch (SPUtils.getInstance("theme", Context.MODE_PRIVATE).getInt("theme", 1)) {
//            case 1:
//                setTheme(R.style.Theme_GZYSmartCity);
//                break;
//            case 2:
//                setTheme(R.style.Theme_GZYSmartCity_Yellow);
//                break;
//            case 3:
//                setTheme(R.style.Theme_GZYSmartCity_Green);
//                break;
//            case 4:
//                setTheme(R.style.Theme_GZYSmartCity_Blue);
//                break;
//        }
        //引导页仅针对未进入过首页（App主页面）的情况下，否则再次进入App时将不再提示启动页，如果登录页面中勾选了自动登录，直接进入首页，否则直接进入登录页
        super.onCreate(savedInstanceState);
        ActivityStartBinding bStart = ActivityStartBinding.inflate(getLayoutInflater());
        setContentView(bStart.getRoot());
        Glide.with(StartActivity.this).load(R.drawable.city).into(bStart.imageUpStart);
        Glide.with(StartActivity.this).load(R.drawable.lenovoedu).into(bStart.imageDownStart);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        sp = getSharedPreferences("start", MODE_PRIVATE);
        String startS = sp.getString("starts", "");
        Handler mHandler = new Handler();
        mHandler.postDelayed(() -> {
            if (startS.equals("")) {
                title.add("智慧医疗");
                title.add("智慧工厂");
                title.add("智慧交通");
                title.add("智慧城市");
                title.add("智慧零售");
                integers.add(R.drawable.banner1);
                integers.add(R.drawable.banner2);
                integers.add(R.drawable.banner3);
                integers.add(R.drawable.banner4);
                integers.add(R.drawable.banner5);

                PopuStartBinding p = PopuStartBinding.inflate(getLayoutInflater(), null, false);
                PopupUntil popupUntil = new PopupUntil();
                popupUntil.getPopupDataByView(p.getRoot(), StartActivity.this);
                StartVPAdapter adapter = new StartVPAdapter(R.layout.item_vp_start);
                p.startVp.setAdapter(adapter);
                adapter.setNewInstance(integers);
                p.startVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                    @Override
                    public void onPageSelected(int position) {
                        super.onPageSelected(position);
                        p.btnStartTyan.setVisibility(View.INVISIBLE);
                        switch (position) {
                            case 0:
                                p.radioStart.check(R.id.radio_btn_1);
                                p.tvStartTitle.setText(title.get(position));
                                p.tvStartTitleTwo.setText(title.get(position) + "Two");
                                break;
                            case 1:
                                p.radioStart.check(R.id.radio_btn_2);
                                p.tvStartTitle.setText(title.get(position));
                                p.tvStartTitleTwo.setText(title.get(position) + "Two");
                                break;
                            case 2:
                                p.radioStart.check(R.id.radio_btn_3);
                                p.tvStartTitle.setText(title.get(position));
                                p.tvStartTitleTwo.setText(title.get(position) + "Two");
                                break;
                            case 3:
                                p.radioStart.check(R.id.radio_btn_4);
                                p.tvStartTitle.setText(title.get(position));
                                p.tvStartTitleTwo.setText(title.get(position) + "Two");
                                break;
                            case 4:
                                p.radioStart.check(R.id.radio_btn_5);
                                p.lineStart.setVisibility(View.GONE);
                                p.btnStartTyan.setVisibility(View.VISIBLE);
                                p.btnStartTyan.setOnClickListener(v -> {
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putString("starts", "yes");
                                    editor.apply();
                                    startActivity(new Intent(StartActivity.this, LoginActivity.class));
                                    popupUntil.closePopup();
                                });
                                break;
                        }
                    }
                });
            } else {
                Intent intent = new Intent(StartActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }, 3000);
    }


}
